package com.aidong.service;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.wills.UploadDao;
import com.aidong.model.wills.Upload;
import com.aidong.util.FuncUtil;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import javax.annotation.Resource;
import javax.imageio.ImageIO;

import com.aidong.web.controller.BaseController;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class UploadService extends BaseController {
    @Resource
    UploadDao uploadDao;

    public UploadService() {
    }

    public Upload upload(MultipartFile file, StringBuilder msg) {
        if(file.isEmpty()) {
            return this.outputMsg(msg, "请上传文件");
        } else {
            String fileName = file.getOriginalFilename();
            String ext = this.getFileExt(fileName);
            if(!"gif".equals(ext) && !"jpg".equals(ext) && !"png".equals(ext)) {
                return this.outputMsg(msg, "只能上传gif,jpg,png格式");
            } else {
                boolean wid = false;
                boolean hei = false;

                String md5;
                int wid1;
                int hei1;
                try {
                    BufferedImage upload = ImageIO.read(file.getInputStream());
                    wid1 = upload.getWidth((ImageObserver)null);
                    hei1 = upload.getHeight((ImageObserver)null);
                    md5 = FuncUtil.Md5(file.getBytes());
                } catch (Exception var13) {
                    return this.outputMsg(msg, "图片格式错误");
                }

                Upload upload1 = this.uploadDao.getByMd5(md5);
                if(upload1 != null) {
                    return upload1;
                } else {
                    upload1 = new Upload();
                    upload1.setFileName(fileName);
                    upload1.setUrl(fileName);
                    upload1.setExt(ext);
                    upload1.setWidth(Integer.valueOf(wid1));
                    upload1.setHeight(Integer.valueOf(hei1));
                    upload1.setSize(Integer.valueOf((int)file.getSize()));
                    upload1.setFileMd5(md5);
                    upload1.setUploadUser(this.getLoginUserCode());
                    upload1.setUploadTime(new Date());

                    try {
                        this.uploadDao.addUpload(upload1);
                        String e = this.getSetting("upload.path");
                        String fn = upload1.getUrl();
                        File dir = new File(e + fn.substring(0, fn.lastIndexOf("/") + 1));
                        if(!dir.exists()) {
                            dir.mkdirs();
                        }

                        file.transferTo(new File(e + fn));
                        return upload1;
                    } catch (Exception var12) {
                        this.log.error("upload", var12);
                        return this.outputMsg(msg, "上传失败");
                    }
                }
            }
        }
    }

    public Upload addUpload(String fileName, byte[] data, StringBuilder msg) {
        String ext = this.getFileExt(fileName);
        if(!"gif".equals(ext) && !"jpg".equals(ext) && !"png".equals(ext)) {
            return this.outputMsg(msg, "只能上传gif,jpg,png格式");
        } else {
            boolean wid = false;
            boolean hei = false;

            String md5;
            int wid1;
            int hei1;
            try {
                ByteArrayInputStream upload = new ByteArrayInputStream(data);
                BufferedImage e = ImageIO.read(upload);
                wid1 = e.getWidth((ImageObserver)null);
                hei1 = e.getHeight((ImageObserver)null);
                md5 = FuncUtil.Md5(data);
            } catch (Exception var14) {
                return this.outputMsg(msg, "图片格式错误");
            }

            Upload upload1 = this.uploadDao.getByMd5(md5);
            if(upload1 != null) {
                return upload1;
            } else {
                upload1 = new Upload();
                upload1.setFileName(fileName);
                upload1.setUrl(fileName);
                upload1.setExt(ext);
                upload1.setWidth(Integer.valueOf(wid1));
                upload1.setHeight(Integer.valueOf(hei1));
                upload1.setSize(Integer.valueOf(data.length));
                upload1.setFileMd5(md5);
                upload1.setUploadUser(this.getLoginUserCode());
                upload1.setUploadTime(new Date());

                try {
                    this.uploadDao.addUpload(upload1);
                    String e1 = this.getSetting("upload.path");
                    String fn = upload1.getUrl();
                    File dir = new File(e1 + fn.substring(0, fn.lastIndexOf("/") + 1));
                    if(!dir.exists()) {
                        dir.mkdirs();
                    }

                    FileOutputStream fos = new FileOutputStream(new File(e1 + fn));
                    fos.write(data);
                    fos.close();
                    return upload1;
                } catch (Exception var13) {
                    this.log.error("addUpload", var13);
                    return this.outputMsg(msg, "上传失败");
                }
            }
        }
    }

    private String getFileExt(String fileName) {
        if(fileName != null) {
            int pos = fileName.lastIndexOf(".");
            return pos > 0?fileName.substring(pos + 1).toLowerCase():"jpg";
        } else {
            return null;
        }
    }

    private Upload outputMsg(StringBuilder sb, String msg) {
        if(sb != null) {
            sb.append(msg);
        }

        return null;
    }
}
